package com.example.controller;

import com.example.entity.Document;
import com.example.service.DocumentApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 文档控制器
 */
@RestController
@RequestMapping("/api/document")
public class DocumentController {
    
    @Autowired
    private DocumentApprovalService documentApprovalService;
    
    /**
     * 提交文档审批
     */
    @PostMapping("/{documentId}/submit")
    public Map<String, String> submitForApproval(
            @PathVariable String documentId,
            @RequestParam String submitter,
            @RequestParam Set<String> approverGroups) {
        
        String processInstanceId = documentApprovalService.startApprovalProcess(
                documentId, 
                submitter, 
                approverGroups);
        
        Map<String, String> result = new HashMap<>();
        result.put("processInstanceId", processInstanceId);
        result.put("message", "文档审批流程已启动");
        return result;
    }
    
    /**
     * 完成审批任务
     */
    @PostMapping("/task/{taskId}/complete")
    public Map<String, String> completeTask(
            @PathVariable String taskId,
            @RequestParam String approver,
            @RequestParam boolean approved,
            @RequestParam(required = false, defaultValue = "") String comment) {
        
        documentApprovalService.completeTask(taskId, approver, approved, comment);
        
        Map<String, String> result = new HashMap<>();
        result.put("message", "审批任务已完成");
        return result;
    }
    
    /**
     * 取消审批流程
     */
    @PostMapping("/{processInstanceId}/cancel")
    public Map<String, String> cancelProcess(
            @PathVariable String processInstanceId,
            @RequestParam String reason) {
        
        documentApprovalService.cancelProcess(processInstanceId, reason);
        
        Map<String, String> result = new HashMap<>();
        result.put("message", "审批流程已取消");
        return result;
    }
}